package com.zhengtoon.mctoon.conv.service.sdzc;

import com.zhengtoon.mctoon.conv.bean.sdzc.Feedback;
import com.zhengtoon.mctoon.conv.bean.sdzc.Letter;
import com.zhengtoon.mctoon.conv.bean.sdzc.Reply;
import com.zhengtoon.mctoon.conv.bean.sdzc.ReplyAttachment;
import com.zhengtoon.mctoon.conv.bean.ws.BaseLetter;
import com.zhengtoon.mctoon.conv.bean.ws.NotAcceptLetter;
import com.zhengtoon.mctoon.conv.bean.ws.ZFHFAppBJTUserLetterService4JKC;
import com.zhengtoon.mctoon.conv.service.sdzc.ZfhfSystemService;
import com.zhengtoon.mctoon.conv.utils.Xml2BeanUtil;
import com.zhengtoon.mctoon.conv.utils.sdzc.ZfhfConstant;
import com.zhengtoon.mctoon.conv.utils.sdzc.ZfhfUtils;
import com.zhengtoon.mctoon.conv.ws.bean.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 中科软系统 政风行风系统服务
 *
 * @author 144637
 */
@Service
public class ZfhfSystemServiceImpl implements ZfhfSystemService {
    private static Logger log = LoggerFactory.getLogger(ZfhfSystemServiceImpl.class);
    @Autowired
    ZFHFAppBJTUserLetterService4JKC zfhfService;

    @Override
    public boolean submitCommunityLetter(Letter letter, boolean haveAttach) {
        // 1.获取请求入参
        String xmlInputParam = ZfhfUtils.getSubmitCommunityLetterParam(letter, haveAttach);
        log.debug("调用接口入参: ===" + xmlInputParam);
        // 2. 请求webservice
        String xmlOutputResult = zfhfService.submitCommunityLetter(xmlInputParam);
        log.debug("调用接口出参: ===" + xmlOutputResult);
        // 3. 解析出参
        LetterResult webServiceResult = Xml2BeanUtil.convert2Bean(xmlOutputResult, LetterResult.class);

        return ZfhfConstant.ZFHF_RESULT_SUCCESS_STATUS.equals(webServiceResult.getResultStatus());
    }

    @Override
    public BaseLetter getCommunityLetterList() {

        // 1.获取请求入参
        String xmlInputParam = ZfhfUtils.getCommunityLetterListParam();
        log.debug("调用接口入参: ===" + xmlInputParam);
        // 2. 请求webservice
        String xmlOutputResult = zfhfService.getCommunityLetterList(xmlInputParam);
        log.debug("调用接口出参: ===" + xmlOutputResult);

        // 3. 解析出参
        LetterResult webServiceResult = Xml2BeanUtil.convert2Bean(xmlOutputResult, LetterResult.class);

        if (ZfhfConstant.ZFHF_RESULT_SUCCESS_STATUS.equals(webServiceResult.getResultStatus())) {

            BaseLetter baseLetter = new BaseLetter();

            List<LetterRow> letterRows = webServiceResult.getLetterInformation().getLetterLists().getFinishLetterList().getRows();
            List<Letter> letters = convertLetterRowToLetter(letterRows);

            List<LetterRow> notAcceptLetterRows = webServiceResult.getLetterInformation().getLetterLists().getNotAcceptLetterList().getRows();
            List<NotAcceptLetter> notAcceptLetters = convertLetterRowToNotAcceptLetter(notAcceptLetterRows);

            baseLetter.setFinishedLetterList(letters);
            baseLetter.setNotAcceptLetterList(notAcceptLetters);
            return baseLetter;
        }
        return null;
    }

    @Override
    public List<Reply> getCommunityLetterReply(String letterId) {
        // 1.获取请求入参
        String xmlInputParam = ZfhfUtils.getCommunityLetterReplyParam(letterId);
        log.debug("调用接口入参: ===" + xmlInputParam);
        // 2. 请求webservice
        String xmlOutputResult = zfhfService.getCommunityLetterReply(xmlInputParam);
        log.debug("调用接口出参: ===" + xmlOutputResult);
        // 3. 解析出参
        ReplyResult webServiceResult = Xml2BeanUtil.convert2Bean(xmlOutputResult, ReplyResult.class);

        if (ZfhfConstant.ZFHF_RESULT_SUCCESS_STATUS.equals(webServiceResult.getResultStatus())) {
            List<Reply> replyList = convertReplyResultToReplyList(webServiceResult);
            return replyList;
        }

        return null;
    }

    @Override
    public ReplyAttachment getCommunityLetterReplyAttachment(String replyId, String attachId) {
        // 1.获取请求入参
        String xmlInputParam = ZfhfUtils.getCommunityLetterReplyAttachmentParam(replyId, attachId);
        log.debug("调用接口入参: ===" + xmlInputParam);
        // 2. 请求webservice
        String xmlOutputResult = zfhfService.getCommunityLetterReplyAttachment(xmlInputParam);
        log.debug("调用接口出参: ===" + xmlOutputResult);
        // 3. 解析出参
        AttachmentResult webServiceResult = Xml2BeanUtil.convert2Bean(xmlOutputResult, AttachmentResult.class);

        String attachmentContent = webServiceResult.getAttachInformation().getAttachment();
        ReplyAttachment replyAttachment = new ReplyAttachment();

        replyAttachment.setReplyAttachContent(attachmentContent);

        return replyAttachment;
    }

    @Override
    public boolean setCommunityLetterFeedbackInfo(Feedback feedback) {

        // 1.获取请求入参
        String xmlInputParam = ZfhfUtils.getSubmitReplyParam(feedback);
        log.debug("调用接口入参: ===" + xmlInputParam);
        // 2. 请求webservice
        String xmlOutputResult = zfhfService.setCommunityLetterFeedbackInfo(xmlInputParam);
        log.debug("调用接口出参: ===" + xmlOutputResult);
        // 3. 解析出参
        LetterResult webServiceResult = Xml2BeanUtil.convert2Bean(xmlOutputResult, LetterResult.class);

        return ZfhfConstant.ZFHF_RESULT_SUCCESS_STATUS.equals(webServiceResult.getResultStatus());
    }

    /**
     * 转换为不受理信件
     *
     * @param letterRows xml解析出的letter对象
     * @return List<NotAcceptLetter>
     * @author 144637
     */
    private List<NotAcceptLetter> convertLetterRowToNotAcceptLetter(List<LetterRow> letterRows) {
        List<NotAcceptLetter> letterList = new ArrayList<>();
        for (LetterRow letterRow : letterRows) {
            NotAcceptLetter letter = new NotAcceptLetter();
            BeanUtils.copyProperties(letterRow, letter);
            letterList.add(letter);
        }
        return letterList;
    }

    /**
     * 类型转换
     *
     * @param letterRows xml解析出的letter对象
     * @return List<Letter>
     * @author 144637
     */
    private List<Letter> convertLetterRowToLetter(List<LetterRow> letterRows) {
        List<Letter> letterList = new ArrayList<>();
        for (LetterRow letterRow : letterRows) {
            Letter letter = new Letter();
            BeanUtils.copyProperties(letterRow, letter);
            letterList.add(letter);
        }
        return letterList;
    }

    /**
     * 将返回的reply result 转换为 replyList   144637
     *
     * @param webServiceResult
     * @return
     */
    private List<Reply> convertReplyResultToReplyList(ReplyResult webServiceResult) {
        List<Reply> replyList = new ArrayList<>();

        List<LetterReplyRow> letterReplyRows = webServiceResult.getReplyInformation().getLetterReplyList().getRows();

        for (LetterReplyRow letterReplyRow : letterReplyRows) {

            List<ReplyRow> replyRows = letterReplyRow.getReplyList().getRows();

            for (ReplyRow replyRow : replyRows) {
                // 1. reply 对象转换
                Reply bean = convertReplyRowToReply(replyRow);

                if (replyRow.getAttachments() != null) {
                    // 处理附件
                    List<ReplyAttachmentRow> replyAttachmentRows = replyRow.getAttachments().getRows();
                    if (!CollectionUtils.isEmpty(replyAttachmentRows)) {
                        List<ReplyAttachment> replyAttachments = new ArrayList<>();
                        for (ReplyAttachmentRow replyAttachmentRow : replyAttachmentRows) {
                            // 附件对象转换
                            ReplyAttachment replyAttachment = new ReplyAttachment();
                            replyAttachment.setReplyAttachId(replyAttachmentRow.getAttachId());
                            replyAttachment.setReplyAttachName(replyAttachmentRow.getAttachName());
                            replyAttachment.setReplyId(replyAttachmentRow.getReplyId());

                            replyAttachments.add(replyAttachment);
                        }
                        // 2. reply 附件列表设置
                        bean.setAttachmentList(replyAttachments);
                    }
                }
                replyList.add(bean);
            }
        }

        return replyList;
    }

    /**
     * 回复row 转换为 reply  144637
     *
     * @param replyRow
     * @return
     */
    private Reply convertReplyRowToReply(ReplyRow replyRow) {

        Reply reply = new Reply();

        reply.setTransferLetterId(replyRow.getTransferLetterId());
        reply.setReplyId(replyRow.getReplyId());
        reply.setReplyType(Integer.parseInt(replyRow.getReplyType()));
        reply.setReplyTitle(replyRow.getReplyTitle());
        reply.setReplyContent(replyRow.getReplyContent());
        reply.setReplyOrgName(replyRow.getReplyOrgName());
        reply.setReplyUser(replyRow.getReplyUser());
        reply.setReplyDate(replyRow.getReplyDate());
        reply.setOpenFlag(Integer.parseInt(replyRow.getOpenFlag()));

        return reply;

    }
}
